﻿Public Class TextBlock : Inherits UIElement

	Dim _Text As String
	Property Text As String
		Get
			Return _Text
		End Get
		Set(value As String)
			_Text = value
			Measure()
			If Not Host Is Nothing Then Host.Remeasure()
		End Set
	End Property

	Dim Size As SizeF

	Property Align As TextAlign = TextAlign.Center

	Enum TextAlign
		Left
		Center
		Right
	End Enum

	Overrides Sub Measure()
		If Font Is Nothing Then Return
		Size = MeasureText(Text, Font)
	End Sub

	Public Overrides ReadOnly Property Height As Integer
		Get
			Return Math.Ceiling(Size.Height) + MyBase.Height
		End Get
	End Property

	Public Overrides ReadOnly Property Width As Integer
		Get
			Return Math.Ceiling(Size.Width) + MyBase.Width
		End Get
	End Property

	Public Overrides Sub Draw(e As System.Windows.Forms.PaintEventArgs)
		MyBase.Draw(e)
		If Font Is Nothing Then Return
		Dim F As New StringFormat
		F.Alignment = StringAlignment.Center
		F.LineAlignment = StringAlignment.Center
		If Align = TextAlign.Left Then F.Alignment = StringAlignment.Near
		e.Graphics.DrawString(Text, Font, New SolidBrush(CType(TextBrush, ColorBrush).Color), ContentRectangle, F)
	End Sub

	Dim _TextBrush As UIBrush
	Property TextBrush As UIBrush
		Get
			If Not _TextBrush Is Nothing Then Return _TextBrush
			Return If(Host Is Nothing, Nothing, New ColorBrush(Host.Parent.ForeColor))
		End Get
		Set(value As UIBrush)
			_TextBrush = value
			Invalidate()
		End Set
	End Property

End Class

Public Class Picture : Inherits UIElement

	Dim _Image As Image
	Property Image As Image
		Get
			Return _Image
		End Get
		Set(value As Image)
			_Image = value
			Measure()
			If Not Host Is Nothing Then Host.Remeasure()
		End Set
	End Property

	Dim Size As SizeF

	Property FixedSize As Drawing.Size

	Overrides Sub Measure()
		If Not Image Is Nothing Then Size = Image.Size
		If FixedSize <> SizeF.Empty Then Size = FixedSize
	End Sub

	Public Overrides ReadOnly Property Height As Integer
		Get
			Return Math.Ceiling(Size.Height) + MyBase.Height
		End Get
	End Property

	Public Overrides ReadOnly Property Width As Integer
		Get
			Return Math.Ceiling(Size.Width) + MyBase.Width
		End Get
	End Property

	Public Overrides Sub Draw(e As System.Windows.Forms.PaintEventArgs)
		MyBase.Draw(e)
		If Image Is Nothing Then Return
		Dim R = ContentRectangle
		If R.Width > Size.Width Then R.Offset((R.Width - Size.Width) / 2, 0)
		If R.Height > Size.Height Then R.Offset(0, (R.Height - Size.Height) / 2)
		R.Size = Size.ToSize
		e.Graphics.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBilinear
		e.Graphics.DrawImage(Image, R)
	End Sub

End Class

Public Class Shape : Inherits UIElement

	Dim _Size As Size
	Property Size As Size
		Get
			Return _Size
		End Get
		Set(value As Size)
			_Size = value
			Measure()
			If Not Host Is Nothing Then Host.Remeasure()
		End Set
	End Property

	Public Overrides ReadOnly Property Height As Integer
		Get
			Return MyBase.Height + Size.Height
		End Get
	End Property

	Public Overrides ReadOnly Property Width As Integer
		Get
			Return MyBase.Width + Size.Width
		End Get
	End Property

End Class